package com.sh4.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sh4.entity.Menu;
import com.sh4.service.MenuService;
import com.sh4.service.RidesCacheService;
import com.sh4.service.impl.CoreServiceImpl;
import com.sh4.util.Result;
import com.sh4.util.wechatUtils.Core;


@RestController
@RequestMapping(value="menu")
public class MenuController {

	@Autowired
//	@Qualifier("")
	MenuService menuService;
	
	@Autowired
	RidesCacheService ridesCacheService;
	
	@RequestMapping(value = "tree",method = RequestMethod.GET)
	public List<Menu> HinsMenu(HttpSession session) {
		
		String roleId = session.getAttribute("roleId").toString();
		return menuService.permissionMenu(roleId);
	}
	
	@RequestMapping(value = "menu",method = RequestMethod.GET)
	public List<Menu> menu(HttpSession session) {
		return menuService.allMenu();
	}
	
	@RequestMapping(value = "tree",method = RequestMethod.POST)
	public Menu saveMenu(@RequestBody Menu menu) {
		
		try {
			menuService.saveMenu(menu);
			
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		
		return menu;
	}
	
	@RequestMapping(value = "tree",method = RequestMethod.DELETE)
	public Result deleteMenu(@RequestBody Menu menu) {
		Result r = new Result(200,"success");
		
		try {
			menuService.deleteMenu(menu);
			
		} catch (Exception e) {
			r.setStatus(200);
			r.setMsg("内部错误");
		}
		
		return r;
		
	}
	
	@RequestMapping(value="clearAllCache")
	public Result clearAllCache() {
		Result r = new Result(200,"success");
		ridesCacheService.clearAllCache();
		
		return r;
	}
	
	@RequestMapping(value="testSql")
	public List<Map<String, Object>> testSql() {
		
		return menuService.testSql();
	}
}
